//
//  WordListView.m
//  EnglishStudyApp
//
//  Created by Nong Trung Nghia on 10/18/12.
//  Copyright (c) 2012 abc. All rights reserved.
//

#import "WordListView.h"
#import "WordListViewModel.h"
#import "WordListViewCell.h"
#import "ESWord.h"
@implementation WordListView
@synthesize model=_model;
@synthesize delegate=_delegate;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor whiteColor];
        _wordsTableView=[[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        _wordsTableView.delegate=self;
        _wordsTableView.dataSource=self;
        [_wordsTableView reloadData];
        [self addSubview:_wordsTableView];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    [_wordsTableView setFrame:self.frame];
}

#pragma mark -
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90.0;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _model.wordList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WordListViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"WordListCellIdentifier"];
    if (!cell) {
        cell=[[WordListViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"WordListCellIdentifier"];
    }
    ESWord *word=[_model.wordList objectAtIndex:indexPath.row];
    cell.word=word;
    return cell;
}

#pragma mark - model
-(void)setModel:(WordListViewModel *)aModel{
    _model=aModel;
    [_wordsTableView reloadData];
}
#pragma mark -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_delegate && [_delegate respondsToSelector:@selector(wordListView:didSelectRowAtIndexPath:)]) {
        [_delegate performSelector:@selector(wordListView:didSelectRowAtIndexPath:) withObject:self withObject:indexPath];
    }
}

@end
